class ScrollContainer extends egret.DisplayObjectContainer {

	private touchD_PT: egret.Point = new egret.Point(0, 0);
	private start_PT: egret.Point = new egret.Point(0, 0);
	// private endX;
	private endPT: egret.Point;
	// 触发滚动的阈值
	private threhold: number = 200;
	public pageIndex: number = 0;
	public arr_child;
	private pageHeight: number = 0;
	private pageMax: number = 0;
	private tw_page: egret.Tween;
	// private sd_manager:SoundManager;

	public constructor() {
		super();
		// this.sd_manager=new SoundManager();
		this.touchEnabled = true;
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
	}
	public init(_arr_child, stageW, stageH): void {
		this.arr_child = _arr_child;
		for (var i = 0; i < this.arr_child.length; i++) {
			this.arr_child[i].x = stageW * i;
			this.addChild(this.arr_child[i]);
		}
		this.pageMax = this.arr_child.length - 1;
	}
	private addToStage(e: egret.Event): void {
		this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchHandler, this);
		this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchHandler, this);
		this.addEventListener(egret.TouchEvent.TOUCH_END, this.touchHandler, this);

	}


	private bool_touchDown: boolean;
	private touchHandler(e: egret.TouchEvent): void {
		var myThis = this;
		e.stopImmediatePropagation();
		console.log('touchHandler')
		if (e.type == 'touchBegin') {
			this.bool_touchDown = true;
			if (this.tw_page) {
				this.tw_page.pause();
				egret.Tween.removeTweens(this.tw_page);
			}
			this.touchD_PT = new egret.Point(e.stageX, e.stageY);
			this.start_PT = new egret.Point(this.x, this.x);

			// myThis.sd_manager.play('effect1_mp3')
		}
		else if (e.type == 'touchMove') {
			if (this.bool_touchDown) {
				if (this.pageIndex == 0 && e.stageX > this.touchD_PT.x) {
					// this.x=this.start_PT.x+(e.stageX - this.touchD_PT.x)/4;
				}
				else if (this.pageIndex == this.pageMax && e.stageX < this.touchD_PT.x) {
					console.log(e.stageX,this.touchD_PT.x,this.start_PT.x);
					// this.x=this.start_PT.x+(e.stageX - this.touchD_PT.x)/4;
				}
				else {
					// this.x += (this.start_PT.x + (e.stageX - this.touchD_PT.x) - this.x) * 0.3;
					this.x=this.start_PT.x+(e.stageX - this.touchD_PT.x);
				}
			}
		}
		else if (e.type == 'touchEnd') {
			this.bool_touchDown = false;
			console.log(this.x, this.pageIndex, this.pageIndex * 1136 - this.threhold)
			if (this.x < -this.pageIndex * 1136 - this.threhold) {
				this.touchEnabled = this.touchChildren = false;
				if (this.pageIndex != this.pageMax) {
					if (this.tw_page) {
						this.tw_page.pause();
						egret.Tween.removeTweens(this.tw_page);
					}
					// myThis.arr_child[myThis.pageIndex].In();
					myThis.dispatchEvent(new ScrollEvent('scrollStart'));
					this.tw_page = egret.Tween.get(this).to({ x: -1136 * (this.pageIndex + 1) }, 700, egret.Ease.cubicOut).call(function (): void {
						myThis.pageIndex++;
						myThis.arr_child[myThis.pageIndex].In();
						myThis.dispatchEvent(new ScrollEvent('scrollComplete'));
						myThis.touchEnabled = myThis.touchChildren = true;
						
					});
				}
				else {
					this.back();
				}

			}
			else if (this.x > -this.pageIndex * 1136 + this.threhold) {
				if (this.pageIndex != 0) {
					this.touchEnabled = this.touchChildren = false;
					if (this.tw_page) {
						this.tw_page.pause();
						egret.Tween.removeTweens(this.tw_page);
					}
					myThis.dispatchEvent(new ScrollEvent('scrollStart'));
					this.tw_page = egret.Tween.get(this).to({ x: -1136 * (this.pageIndex - 1) }, 700, egret.Ease.cubicOut).call(function (): void {
						myThis.pageIndex--;
						// myThis.arr_child[myThis.pageIndex].In();
						myThis.dispatchEvent(new ScrollEvent('scrollComplete'));
						myThis.touchEnabled = myThis.touchChildren = true;
					});
				}
				else {
					this.back();
				}
			}
			else {
				this.back();
			}
		}
	}
	private back(): void {
		console.log('back');
		if (this.tw_page) {
			this.tw_page.pause();
			egret.Tween.removeTweens(this.tw_page);
		}
		console.log('false');
		var time = 1000 * Math.abs(this.x - (this.pageIndex * -1136)) / 400;

		this.touchEnabled = this.touchChildren = false;
		var myThis = this;
		this.tw_page = egret.Tween.get(this).to({ x: this.pageIndex * -1136 }, time, egret.Ease.cubicOut).call(function (): void {
			myThis.touchEnabled = myThis.touchChildren = true;
			console.log('false')
		});
	}
}